package www.com.springboot_20200312.rabbitmqDemo.HeadersExchange.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import www.com.springboot_20200312.rabbitmqDemo.HeadersExchange.message.DemoHeadersMessage;


/**
 * @program: springboot_20200312
 * @ClassName RabbitConfig
 * @description:
 * @author: jht
 * @create: 2021-12-07 10:20
 * @Version 1.0
 **/
@Configuration
public class RabbitConfigHeaders {

    public static class HeadersExchangeDemoConfiguration {

        // 创建 Queue
        @Bean
        public Queue demo04Queue() {
            return new Queue(DemoHeadersMessage.QUEUE, // Queue 名字
                    true, // durable: 是否持久化
                    false, // exclusive: 是否排它
                    false); // autoDelete: 是否自动删除
        }

        // 创建 Headers Exchange
        @Bean
        public HeadersExchange demo04Exchange() {
            return new HeadersExchange(DemoHeadersMessage.EXCHANGE,
                    true,  // durable: 是否持久化
                    false);  // exclusive: 是否排它
        }

        // 创建 Binding
        // Exchange：Demo04Message.EXCHANGE
        // Queue：Demo04Message.QUEUE
        // Headers: Demo04Message.HEADER_KEY + Demo04Message.HEADER_VALUE
        @Bean
        public Binding demo4Binding() {
            return BindingBuilder.bind(demo04Queue()).to(demo04Exchange())
                    .where(DemoHeadersMessage.HEADER_KEY).matches(DemoHeadersMessage.HEADER_VALUE); // 配置 Headers 匹配
        }

    }
}
